package cn.bytecollege.admin.filter; /**
 * @Auther：MR.P
 * @Date:2021/10/28 16 19
 * @Description:对添加分类和修改的请求进行过滤
 */

import cn.bytecollege.common.entity.Classify;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "ClassifyFilter",servletNames = {"ClassifySaveController","ClassifyUpdateController"})
public class ClassifyFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        String method = req.getMethod();
        //对post请求进行过滤
        if(method.equalsIgnoreCase("post")){
            String categoryName = req.getParameter("classifyName");
            if(categoryName==null||categoryName.equals("")){
                String msg = "请填写分类名称！";
                request.setAttribute("msg",msg);
                if(uri.contains("classifySave")){
                    request.getRequestDispatcher("/admin/html/device/classifySave.jsp").forward(request,response);
                    return;
                }else if(uri.contains("classifyUpdate")){
                    String id = req.getParameter("classifyId");
                    Integer classifyId = Integer.valueOf(id);
                    String classifyName = req.getParameter("classifyName");
                    //把原来的数据返回去
                    Classify tempClassify = new Classify();
                    tempClassify.setClassifyName(classifyName);
                    tempClassify.setClassifyId(classifyId);
                    request.setAttribute("classify",tempClassify);
                    request.getRequestDispatcher("/admin/html/device/classifyUpdate.jsp").forward(request,response);
                    return;
                }
                return;
            }
            //如果分类名称不为null也不为空，则放行
            chain.doFilter(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
}
